<template>
    <div>
        <div class="form-item form-item-style1 paddingR12">
            <div class="label-title">
                <label>单位名称</label>
            </div>
            <p style="display:flex;align-items: center;"
               @click="companyPropShow = true">
                <span style="color:#999;font-size:14px;">{{departObj.Name}}</span>
                <x-icon type="ios-arrow-right"
                        fill="#c8c8cd"
                        size="24"></x-icon>
            </p>
        </div>
        <div class="form-item  form-item-style1 paddingR12">
            <div class="label-title">
                <label>带班领导</label>
            </div>
            <p style="display:flex;align-items: center;"
               @click="copyUserNameShow = true">
                <span style="color:#999;font-size:14px;">{{teamLeader.Name}}</span>
                <x-icon type="ios-arrow-right"
                        fill="#c8c8cd"
                        size="24"></x-icon>
            </p>
        </div>
        <div class="form-item  form-item-style1">
            <div class="label-title">
                <label>开始时间</label>
            </div>
            <group>
                <datetime v-model="startTime"></datetime>
            </group>
        </div>
        <div class="form-item  form-item-style1">
            <div class="label-title">
                <label>结束时间</label>
            </div>
            <group>
                <datetime v-model="endTime"></datetime>
            </group>
        </div>
        <h2>总次数: {{allCount}}</h2>
        <noData v-if="listAllCount == 0"></noData>
        <div class="bscroll">
            <vue-better-scroll class="wrapper"
                               ref="scroll"
                               :scrollbar="scrollbarObj"
                               :pullDownRefresh="pullDownRefreshObj"
                               :pullUpLoad="pullUpLoadObj"
                               :startY="parseInt(startY)"
                               @pullingDown="onPullingDown"
                               @pullingUp="onPullingUp">
                <div class="bscroll-container">
                    <ul class="lists">
                        <li v-for="(item,index) in list"
                            :key="index">
                            <div>
                                <p>{{item.DepartName}}</p>
                            </div>
                            <div class="status">
                                <span>带班{{item.RecordCount}}次</span>
                            </div>
                        </li>
                    </ul>
                </div>
            </vue-better-scroll>
        </div>
        <copyUserNameProp :copyUserNameShow="copyUserNameShow"
                          :selectCopyUserName="selectLeaderFn"
                          :initTheModel="{Id: '',Name: '全部',Children:[]}"
                          :single="true"></copyUserNameProp>
        <companyProp :companyPropShow="companyPropShow"
                     :selectCompany="selectCompany"
                     business="GetHdProjectsAndFb"></companyProp>
        <myMenu></myMenu>
    </div>
</template>
<script>
import formatDate from '@/utils/formatDate'
import companyProp from '@/commonComponents/companyProp'
import VueBetterScroll from 'vue2-better-scroll'
import { TransferDom, Popup } from 'vux'
import myMenu from '@/commonComponents/menu'
import copyUserNameProp from '@/commonComponents/copyUserNameProp'
export default {
    name: 'statistical',
    directives: {
        TransferDom
    },
    components: {
        Popup,
        VueBetterScroll,
        companyProp,
        copyUserNameProp,
        myMenu
    },
    data () {
        return {
            // dropDown: false
            // 这个配置可以开启滚动条，默认为 false。当设置为 true 或者是一个 Object 的时候，都会开启滚动条，默认是会 fade 的
            scrollbarObj: {
                fade: true
            },
            // 这个配置用于做下拉刷新功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启下拉刷新，可以配置顶部下拉的距离（threshold） 来决定刷新时机以及回弹停留的距离（stop）
            pullDownRefreshObj: {
                threshold: 90,
                stop: 40
            },
            // 这个配置用于做上拉加载功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启上拉加载，可以配置离底部距离阈值（threshold）来决定开始加载的时机
            pullUpLoadObj: {
                threshold: 0,
                txt: {
                    more: '加载更多',
                    noMore: '没有更多数据了'
                }
            },
            startY: 0, // 纵轴方向初始化位置
            scrollToX: 0,
            scrollToY: 0,
            scrollToTime: 700,
            list: [],
            allCount: 0, // 带班总次数
            listAllCount: 0,
            listCount: 0,
            companyPropShow: false,
            startTime: `${formatDate().split('-')[0]}-${formatDate().split('-')[1]}-01`,
            endTime: formatDate(),
            departObj: {
                Name: '',
                Id: ''
            },
            teamLeader: {
                Name: '请选择',
                Id: ''
            },
            userInfo: '',
            copyUserNameShow: false
        }
    },
    created () {
        this.userInfo = JSON.parse(localStorage.getItem('userInfo'))
        this.departObj.Name = this.userInfo.deptname
        this.departObj.Id = this.userInfo.deptid
    },
    mounted () {
        this.onPullingDown()
    },
    methods: {
        onPullingDown () {
            this.pageIndex = 0
            this.allCount = 0
            this.list = []
            this.getData()
        },
        onPullingUp () {
            this.pageIndex++
            this.getData()
        },
        selectLeaderFn (item) {
            this.copyUserNameShow = false
            this.teamLeader = item
            this.onPullingDown()
        },
        selectCompany (item) {
            this.companyPropShow = false
            this.departObj = item
            this.onPullingDown()
        },
        getData () {
            let params = {
                'business': 'GetWorkRecord',
                'data': {
                    DepartID: this.departObj.Id,
                    BusinessType: '0',
                    WorkLeader: this.teamLeader.Id,
                    startDate: this.startTime,
                    EndDate: this.endTime
                },
                'pageIndex': this.pageIndex,
                'pageSize': 30,
                'userId': this.userInfo.userid
            }

            this.$axiosAjax.getConfigJson(params)
                .then((res) => {
                    if (res.data.code === 0) {
                        this.list = this.list.concat(res.data.data)
                        this.listAllCount = res.data.count
                        this.listCount = this.list.length
                    }
                    this.list.forEach(item => {
                        this.allCount += item.RecordCount
                    })
                    if (this.listCount < this.listAllCount) {
                        this.$refs.scroll.forceUpdate(true)
                    } else {
                        this.$refs.scroll.forceUpdate(false)
                    }
                }).catch((response) => {
                })
        }
    }
}
</script>
<style scoped>
h2 {
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: right;
    padding-right: 20px;
    box-sizing: border-box;
    background: #ebeef2;
}
.bscroll {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    top: 320px;
    overflow: hidden;
}
.lists {
    padding-left: 20px;
}
.lists li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px 14px 0;
    border-bottom: 1px solid #efeff4;
}
.lists p {
    font-size: 14px;
    color: #333;
    line-height: 18px;
    max-width: 270px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.status {
    display: flex;
    align-items: center;
}
.status span {
    font-size: 12px;
}
</style>
